<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Follow
 * @description : Follow a tweet to get updates via tweetBuddy
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Follow extends AUTH_Controller {

  public function index() {

    // Checking request type
    if ($this->is_post() && $this->is_ajax()) {

      // Init response var
      $response = array('status' => '0', 'count' => '0', 'data' => '');

      // Getting & cleaning inputs
      $tweet_id = $this->input->post('tweet_id', TRUE);
      $action_type = $this->input->post('action_type', TRUE);
      $action_type = ($action_type == 1) ? $action_type : 0;

      // Checking tweet id
      if (!empty($tweet_id) && is_numeric($tweet_id)) {

        // Getting tweet details
        $get_tweet_by_id = $this->tweet_model->get_tweet_by_id($tweet_id);

        // Checking tweet details
        if ($get_tweet_by_id['status'] == 1 && !empty($get_tweet_by_id['result'])) {

          // Checking action type (Follow)
          if ($action_type == '1') {

            // Insert to db
            $add_follow = $this->tweet_model->add_follow($tweet_id, $this->current_user['user_id']);

            // Checking insertion status
            if ($add_follow['status'] == 1 && !empty($add_follow['result'])) {

              // Setting response status
              $response['status'] = '1';
            }
          }

          // Checking action type (Unfollow)
          if ($action_type == '0') {

            // Deleted from db
            $del_follow = $this->tweet_model->del_follow($tweet_id, $this->current_user['user_id']);

            // Checking deletion status
            if ($del_follow['status'] == 1 && !empty($del_follow['result'])) {

              // Setting response status
              $response['status'] = '1';
            }
          }
        }
      }

      // Responding json
      die(json_encode($response));
    }

    // Redirect if something wrong
    redirect();
  }

}

?>